<?php


namespace Drupal\youji_userwxinfo\ViewsController\handler\field;


use views_handler_field;

class YoujiUserwxinfoAvatarViewsField extends views_handler_field {


  /**
   * {@inheritdoc}
   */
  public function option_definition() {
    $options = parent::option_definition();
    $options['link_photo_to_profile'] = ['default' => TRUE, 'bool' => TRUE];
    $options['image_style'] = ['default' => ''];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    if (module_exists('image')) {
      $styles = image_styles();
      $style_options = ['' => t('Default')];
      foreach ($styles as $style) {
        $style_options[$style['name']] = $style['name'];
      }

      $form['image_style'] = [
        '#title' => t('Image style'),
        '#description' => t('Using <em>Default</em> will use the site-wide image style for user pictures set in the <a href="!account-settings">Account settings</a>.', ['!account-settings' => url('admin/config/people/accounts', ['fragment' => 'edit-personalization'])]),
        '#type' => 'select',
        '#options' => $style_options,
        '#default_value' => $this->options['image_style'],
      ];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function render($values) {
    $output = '';
    if ($this->options['image_style'] && module_exists('image')) {
      $picture_filepath = variable_get('user_picture_default', '');
      if ($picture_fid = $this->get_value($values)) {
        if (!empty($picture_fid)) {
          $picture = file_load($picture_fid);
          if (!empty($picture)) {
            $picture_filepath = $picture->uri;
          }
        }
      }
      if (file_valid_uri($picture_filepath)) {
        $output = theme('image_style', [
          'style_name' => $this->options['image_style'],
          'path' => $picture_filepath,
        ]);
      }
    }
    return $output;
  }

}
